home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / timer11.zip / ZTIMER.H < prev    next >
C/C++ Source or Header  |  1992-04-21  |  8KB  |  262 lines

  1. /****************************************************************************
  2. *
  3. *                           The Zen Timer Library
  4. *
  5. *                               From the book
  6. *                         "Zen of Assembly Language"
  7. *                            Volume 1, Knowledge
  8. *
  9. *                             by Michael Abrash
  10. *
  11. *                      Modifications by Kendall Bennett
  12. *
  13. * Filename:        $RCSfile: ztimer.h $
  14. * Version:        $Revision: 1.9 $
  15. *
  16. * Language:        ANSI C, C++ 2.1
  17. * Environment:    IBM PC (MS DOS)
  18. *
  19. * Description:    Header file for the Zen Timer library. Provides a number
  20. *                of routines to accurately time segments of code. A
  21. *                precision timer is provided for highly accurate timing of
  22. *                code that takes less than 54 ms to execute, and a long
  23. *                period timer is provided to time code that takes up to
  24. *                one hour to execute. All output is in microseconds.
  25. *
  26. *                The ultra long period timer can be used to time code
  27. *                that takes up to 24 hours to execute (raytracing
  28. *                etc).
  29. *
  30. *                We also now provide a set of C++ classes to manipulate
  31. *                the Zen Timers. Note that you can only have one timer
  32. *                running at a time!! Note that all of the code is inline
  33. *                to make it memory model independant, and to avoid the
  34. *                extra cost of a function call overhead in the C++ wrappers.
  35. *
  36. * $Id: ztimer.h 1.9 92/04/21 01:19:51 kjb release $
  37. *
  38. * Revision History:
  39. * -----------------
  40. *
  41. * $Log:    ztimer.h $
  42. * Revision 1.9  92/04/21  01:19:51  kjb
  43. * Converted to memory model dependant library.
  44. * Revision 1.8  92/04/21  00:47:55  kjb
  45. * Fixed code to be memory model independant.
  46. * Revision 1.7  92/04/20  23:30:52  kjb
  47. * Cosmetic changes.
  48. * Revision 1.6  92/04/20  17:35:07  kjb
  49. * Added C++ classes to manipulate the timers.
  50. * ./
  51. * Revision 1.5  92/01/27  21:40:01  kjb
  52. * Converted to a memory model independant library, and released to the
  53. * public.,
  54. *
  55. * Revision 1.4  91/12/31  19:34:51  kjb
  56. * Changed include file directories.
  57. *
  58. * Revision 1.3  91/12/26  17:56:38  kjb
  59. * Added dependency on DEBUG.H
  60. *
  61. * Revision 1.2  91/11/16  17:12:20  kjb
  62. * Modified to return a long integer representing the count rather than a
  63. * string.
  64. *
  65. * Revision 1.1  91/11/14  17:19:58  kjb
  66. * Initial revision
  67. *
  68. ****************************************************************************/
  69.  
  70. #ifndef    __ZTIMER_H
  71. #define    __ZTIMER_H
  72.  
  73. #ifndef __DEBUG_H
  74. #include "debug.h"
  75. #endif
  76.  
  77. /*-------------------------- Function Prototypes --------------------------*/
  78.  
  79. #ifdef    __cplusplus
  80. extern "C" {            /* Use "C" linkage when in C++ mode    */
  81. #endif
  82.  
  83. /* Precision timing routines in PZTIMER.ASM */
  84.  
  85. void    far PZTimerOn(void);
  86. void    far PZTimerOff(void);
  87. void    far PZTimerReport(void);
  88. ulong    far PZTimerCount(void);
  89.  
  90. /* Long period timing routines in LZTIMER.ASM */
  91.  
  92. void     far LZTimerOn(void);
  93. void     far LZTimerOff(void);
  94. void     far LZTimerReport(void);
  95. ulong     far LZTimerCount(void);
  96.  
  97. /* Ultra long period timing routines in TIMER.C */
  98.  
  99. ulong    ULZReadTime(void);
  100. ulong    ULZElapsedTime(ulong start,ulong finish);
  101.  
  102. #ifdef    __cplusplus
  103. }                        /* End of "C" linkage for C++    */
  104. #endif
  105.  
  106. /*--------------------------- Class Definitions ---------------------------*/
  107.  
  108. #ifdef    __cplusplus
  109.  
  110. #ifndef    __IOSTREAM_H
  111. #include <iostream.h>
  112. #endif
  113.  
  114. //---------------------------------------------------------------------------
  115. // Precision Zen Timer class. This can be used to time code that takes up
  116. // to 54 ms to execute between calls to start() and stop() or lap(). The
  117. // aggregate count can be up to 2^32 - 1 microseconds (about 1 hour
  118. // and 10 mins).
  119. //---------------------------------------------------------------------------
  120.  
  121. class PZTimer {
  122. protected:
  123.     ulong    _count;                // Running count
  124.     short    _overflow;            // Flags an overflow
  125.  
  126. public:
  127.             // Constructor
  128.             PZTimer()        { _count = 0; _overflow = false; };
  129.  
  130.             // Method to start the timer
  131.             void start()    { PZTimerOn(); };
  132.  
  133.             // Method to restart the timer
  134.             void restart()    { reset(); start(); };
  135.  
  136.             // Method to stop the timer
  137.             void stop()
  138.             {
  139.                 PZTimerOff();
  140.                 if (!overflow()) {
  141.                     ulong newcount = _count + PZTimerCount();
  142.                     if (newcount < _count || newcount == 0xFFFFFFFF)
  143.                         _overflow = true;
  144.                     else
  145.                         _count = newcount;
  146.                     }
  147.             };
  148.  
  149.             // Method to return the current count
  150.             ulong count()    { return overflow() ? 0xFFFFFFFF : _count; };
  151.  
  152.             // Method to reset the timer to a zero count
  153.             void reset()    { _count = 0; _overflow = false; };
  154.  
  155.             // Method to determine if overflow occurred
  156.             bool overflow()    { return _overflow; };
  157.  
  158.             // Method to return timer resolution (counts in a second).
  159.             ulong resolution()    { return 1000000L; };
  160.  
  161.             // Method to display the timed count in seconds
  162.     friend    ostream& operator << (ostream& o,PZTimer& timer);
  163.     };
  164.  
  165. //---------------------------------------------------------------------------
  166. // Long Period Zen Timer class. This can be used to time code that takes up
  167. // to 1 hour to execute between calls to start() and stop() or lap(). The
  168. // aggregate count can be up to 2^32 - 1 microseconds (about 1 hour
  169. // and 10 mins).
  170. //---------------------------------------------------------------------------
  171.  
  172. class LZTimer {
  173. protected:
  174.     ulong    _count;
  175.     short    _overflow;
  176. public:
  177.             // Constructor
  178.             LZTimer()        { _count = 0; _overflow = false; };
  179.  
  180.             // Method to start the timer
  181.             void start()    { LZTimerOn(); };
  182.  
  183.             // Method to restart the timer
  184.             void restart()    { reset(); start(); };
  185.  
  186.             // Method to stop the timer
  187.             void stop()
  188.             {
  189.                 LZTimerOff();
  190.                 if (!overflow()) {
  191.                     ulong newcount = _count + LZTimerCount();
  192.                     if (newcount < _count || newcount == 0xFFFFFFFF)
  193.                         _overflow = true;
  194.                     else
  195.                         _count = newcount;
  196.                     }
  197.             };
  198.  
  199.             // Method to return the current count
  200.             ulong count()    { return overflow() ? 0xFFFFFFFF : _count; };
  201.  
  202.             // Method to reset the timer to a zero count
  203.             void reset()    { _count = 0; _overflow = false; };
  204.  
  205.             // Method to determine if overflow occurred
  206.             bool overflow()    { return _overflow; };
  207.  
  208.             // Method to return timer resolution (counts in a second).
  209.             ulong resolution()    { return 1000000L; };
  210.  
  211.             // Method to display the timed count in seconds
  212.     friend    ostream& operator << (ostream& o,LZTimer& timer);
  213.     };
  214.  
  215. //---------------------------------------------------------------------------
  216. // Ultra Long Period Zen Timer class. This can be used to time code that
  217. // takes up 24 hours total to execute between calls to start() and stop().
  218. // The aggregate count can be up to 2^32 - 1 tenths of a second, which
  219. // is about 119,000 hours! Should be enough for most applications.
  220. //---------------------------------------------------------------------------
  221.  
  222. class ULZTimer {
  223. protected:
  224.     ulong    _count,_start,_finish;
  225.     short    _overflow;
  226. public:
  227.             // Constructor
  228.             ULZTimer()        { _count = 0; _overflow = false; };
  229.  
  230.             // Method to start the timer
  231.             void start()    { _start = ULZReadTime(); };
  232.  
  233.             // Method to restart the timer
  234.             void restart()    { reset(); start(); };
  235.  
  236.             // Method to stop the timer
  237.             void stop();
  238.  
  239.             // Method to return the current count
  240.             ulong count()    { return overflow() ? 0xFFFFFFFF : _count; };
  241.  
  242.             // Method to reset the timer to a zero count
  243.             void reset()    { _count = 0; _overflow = false; };
  244.  
  245.             // Method to determine if overflow occurred
  246.             bool overflow()    { return _overflow; };
  247.  
  248.             // Method to return timer resolution (counts in a second).
  249.             ulong resolution()    { return 10L; };
  250.  
  251.             // Method to display the timed count in seconds
  252.     friend    ostream& operator << (ostream& o,ULZTimer& timer);
  253.     };
  254.  
  255. #endif
  256.  
  257. #endif    __ZTIMER_H
  258.